package com.bxait.toymail.seckill.controller;

import com.bxait.com.utils.R;
import com.bxait.toymail.seckill.service.SecKillService;
import com.bxait.toymail.seckill.to.SecKillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
@description:
@ClassName SecKillController
@author Chen
@create 2023-04-30 16:00
@Version 1.0
*/
@Slf4j
@Controller
public class SecKillController {


    @Resource
    @Qualifier("secKillService")
    SecKillService seckillService;

    /*
     * @param
     * @==============History===============<br/>
     * @Description // 获取当前时间内的秒杀商品信息
     * @Date 2023/4/30 2023/4/30
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return com.bxait.com.utils.R
     */
    @ResponseBody
    @GetMapping("/currentSecKillSkus")
    public R getCurrentSecKillSkus(){
        List<SecKillSkuRedisTo> vos = seckillService.getCurrentSecKillSkus();
        return R.ok().setData(vos);
    }


    /*
     * @param skuId
     * @==============History===============<br/>
     * @Description //   获取当前商品的秒杀信息
     * @Date 2023/5/4 2023/5/4
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return com.bxait.com.utils.R
     */
    @ResponseBody
    @GetMapping("/sku/secKill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisTo to =  seckillService.getSkuSecKillInfo(skuId);
        return R.ok().setData(to);
    }


    /*
     * @param killId    需要秒杀的商品id
     * @param key       秒杀随机码
     * @param num       需要秒杀的数量
     * @param model
     * @==============History===============<br/>
     * @Description //   秒杀商品
     * @Date 2023/5/4 2023/5/4
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return java.lang.String
     */
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        String orderSn =  seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        // 判断是否登录
        return "success";
    }




    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }


}
